<template>
  <div>
    <BreakSpace label="基本信息" :containerStyle="{ marginBottom: '15px' }" />
    <FormPanel
      ref="formPanel"
      :list="formList"
      labelWidth="100"
      :cols="1"
      :formType="type"
      :isSubmitBtn="false"
      @formChange="formChangeHandle"
    />
    <BaseDialog
      :visible.sync="issearch"
      :title="'配件查询'"
      destroyOnClose
      :containerStyle="{height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px'}"
    >
      <part-infos  @close="partClose" />
    </BaseDialog>
    <div
      :style="{
        textAlign: 'center'
      }"
    >
      <el-button @click="saveDialog" :loading="saveLoading" type="primary">保 存</el-button>
      <el-button @click="closeDialog">返 回</el-button>
    </div>
  </div>
</template>

<script>
import { dictionary } from '@/utils/dictMixin';
import {updateById} from '@/api/partsmanage/partinfos/partInfoManage/index';
import partInfos from "./partInfos";

export default {
  name: "updatePartSort",
  mixins: [dictionary],
  components: {partInfos},
  props: {
    type: {
      type: String,
      default: 'add',
    },
    rowData: {
      type: Object,
      default: () => ({})
    }
  },
  data() {
    this.saveData = null;
    return {
      saveLoading: false,
      formList: this.createFormList(),
      issearch: false,
    };
  },
  created() {
    this.init();
  },
  methods: {
    selectChange(val) {
      console.log('val', val);
      this.rowSelectData = val;
    },
    init() {
      console.info(this.type)
      console.info(this.rowData)
      if (this.type == 'edit'){
        this.formList.find(item=>item.fieldName=="bjxmbh").disabled=true
        this.formList.find(item=>item.fieldName=="bjpjfl").rules=null
        this.formList.forEach(x => {
          x.initialValue = this.rowData[x.fieldName];
        });
      }
    },
    createFormList() {
      return [
        {
          type: 'INPUT',
          label: '配件代码',
          fieldName: 'bjxmbh',
          readonly:true,
          rules: [{ required: true, message: '请选择配件', trigger: 'blur' }],
          initialValue: '',
          unitRender: () => {
            return (
              <div>
                <el-button icon="el-icon-search" onClick={() => this.seacherFun()}></el-button>
              </div>
            );
          },
        },
        {
          type: 'INPUT',
          label: '配件名称',
          fieldName: 'bjzwmc',
          disabled: true,
          initialValue: '',
        },
        {
          type: 'SELECT',
          label: '配件属性',
          fieldName: 'bjpjsx',
          itemList: this.createDictList('5000'),
          disabled: true,
        },
        {
          type: 'SELECT',
          label: '配件分类',
          fieldName: 'bjpjfl',
          itemList:this.createDictList('5017'),
          rules: [{ required: true, message: '请选择配件分类', trigger: 'blur' }],
        }
      ]
    },
    formChangeHandle(val) {
      console.info(val)
      this.saveData = val;
    },
    partClose(val){
      console.info(val)
      if(val){
        console.info('1111111111')
        this.formList.find(x=>x.fieldName=='bjxmbh').initialValue=val[0].bjxmbh;
        this.formList.find(x=>x.fieldName=='bjzwmc').initialValue=val[0].bjzwmc;
        this.formList.find(x=>x.fieldName=='bjpjsx').initialValue=val[0].bjpjsx;
        this.rowData.id = val[0].id;
      }
      this.issearch=false;
    },
    seacherFun(){
      this.issearch = true
    },
    async saveDialog() {
      console.info(this.saveData)
      console.info(this.rowData)
      console.info('11111111111111111111')
      const err = this.$refs.formPanel.SUBMIT_FORM();
      if (err === null) {
        return false
      }
      this.saveLoading = true
      this.saveData.id = this.rowData.id
      const res = await updateById(this.saveData);
      if (res.resultCode === 200 ) {
        this.$notify.success({title: '提示信息', message: '保存成功'});
        this.closeDialog('refresh');
      }
      this.saveLoading = false
    },
    closeDialog(param) {
      this.$emit('close', {visible: false, type: param});
    }
  }
}
</script>

<style scoped>

</style>
